/*=========================================================================

  Library:   HMP

  Copyright (c) Kitware Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=========================================================================*/

// Qt includes
#include <QFile>
#include <QTextStream>

// HMP includes
#include "FileLogger.h"

// --------------------------------------------------------------------------
// FileLoggerPrivate

// --------------------------------------------------------------------------
class FileLoggerPrivate
{
  Q_DECLARE_PUBLIC(FileLogger);
protected:
  FileLogger* const q_ptr;
public:
  FileLoggerPrivate(FileLogger& object);
  ~FileLoggerPrivate();

  void init();

  bool Enabled;
  QString FilePath;
  int NumberOfFilesToKeep;
};

// --------------------------------------------------------------------------
FileLoggerPrivate::FileLoggerPrivate(FileLogger& object)
  : q_ptr(&object)
{
  this->Enabled = true;
  this->NumberOfFilesToKeep = 10;
}

// --------------------------------------------------------------------------
FileLoggerPrivate::~FileLoggerPrivate()
{
}

// --------------------------------------------------------------------------
void FileLoggerPrivate::init()
{
}

// --------------------------------------------------------------------------
// FileLogger

// --------------------------------------------------------------------------
FileLogger::FileLogger(QObject* parentObject)
  : Superclass(parentObject)
  , d_ptr(new FileLoggerPrivate(*this))
{
  Q_D(FileLogger);
  d->init();
}

// --------------------------------------------------------------------------
FileLogger::~FileLogger()
{
}

// --------------------------------------------------------------------------
bool FileLogger::enabled()const
{
  Q_D(const FileLogger);
  return d->Enabled;
}

// --------------------------------------------------------------------------
void FileLogger::setEnabled(bool value)
{
  Q_D(FileLogger);
  d->Enabled = value;
}

// --------------------------------------------------------------------------
QString FileLogger::filePath()const
{
  Q_D(const FileLogger);
  return d->FilePath;
}

// --------------------------------------------------------------------------
void FileLogger::setFilePath(const QString& filePath)
{
  Q_D(FileLogger);
  d->FilePath = filePath;
}

// --------------------------------------------------------------------------
int FileLogger::numberOfFilesToKeep()const
{
  Q_D(const FileLogger);
  return d->NumberOfFilesToKeep;
}

// --------------------------------------------------------------------------
void FileLogger::setNumberOfFilesToKeep(int value)
{
  Q_D(FileLogger);
  d->NumberOfFilesToKeep = value;
}

// --------------------------------------------------------------------------
void FileLogger::logMessage(const QString& msg)
{
  Q_D(FileLogger);
  if (!d->Enabled)
    {
    return;
    }
  QFile f(d->FilePath);
  if (!f.open(QFile::Append))
    {
    return;
    }
  QTextStream s(&f);
  s << msg << endl;
  f.close();
}

